cmake_minimum_required(VERSION 3.16)
project(data_reflow)  # 确保与package.xml中的名称一致

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(data_reflow_interfaces REQUIRED)  # 依赖自定义接口包
find_package(yaml-cpp REQUIRED)                # 用于配置文件解析
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)

# 头文件目录（包含include/data_reflow）
include_directories(
  include
)

# 1. 配置管理器库（config_manager）
add_library(config_manager SHARED
  src/config_manager/config_manager.cpp
)
ament_target_dependencies(config_manager
  rclcpp
  yaml-cpp
  std_msgs
)
target_link_libraries(config_manager
  yaml-cpp  # 链接yaml库
)
target_include_directories(config_manager
  PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

# 2. 数据采集器库（data_catcher）
add_library(data_catcher SHARED
  src/data_catcher/data_catcher.cpp
)
ament_target_dependencies(data_catcher
  rclcpp
  sensor_msgs
  data_reflow_interfaces
)
target_link_libraries(data_catcher
  config_manager  # 依赖配置管理器库
  data_buffer
)

# 3. 配置管理器测试节点
add_executable(test_config_manager
  src/config_manager/test_config_manager.cpp
)
ament_target_dependencies(test_config_manager
  rclcpp
)
target_link_libraries(test_config_manager
  config_manager
)

# 4. 数据采集器测试节点
add_executable(test_data_catcher_node
  src/data_catcher/test_data_catcher_node.cpp
)
ament_target_dependencies(test_data_catcher_node
  rclcpp
  sensor_msgs
  data_reflow_interfaces
)
target_link_libraries(test_data_catcher_node
  config_manager
  data_catcher
  data_buffer
)
# 5.缓存区 (data_buffer)
add_library(data_buffer SHARED
  src/data_buffer/data_buffer.cpp
)
ament_target_dependencies(data_buffer
  rclcpp
  std_msgs
  data_reflow_interfaces
)
target_link_libraries(data_buffer
  config_manager
)
# 6.事件检测区 (event_monitor)
add_library(event_subscriber SHARED
  src/event_monitor/event_subscriber.cpp
)
ament_target_dependencies(event_subscriber
  rclcpp
  std_msgs
  data_reflow_interfaces
)
target_link_libraries(event_subscriber
  config_manager
  data_buffer
)
# 7.事件测试节点
add_executable(test_event_subscriber_node
  src/event_monitor/test_event_subscriber_node.cpp
)
ament_target_dependencies(test_event_subscriber_node
  rclcpp
  std_msgs
  data_reflow_interfaces
)
target_link_libraries(test_event_subscriber_node
  event_subscriber
  data_buffer
  config_manager
)
# 8.事件发布节点
add_executable(collision_event_publisher
  src/event_monitor/collision_event_publisher.cpp
)
ament_target_dependencies(collision_event_publisher
  rclcpp
  std_msgs
  data_reflow_interfaces
)
target_link_libraries(collision_event_publisher
  event_subscriber
  data_buffer
  config_manager
)

# 安装库文件
install(TARGETS
  config_manager
  data_catcher
  data_buffer
  event_subscriber
  LIBRARY DESTINATION lib
)

# 安装可执行节点
install(TARGETS
  test_config_manager
  test_data_catcher_node
  test_event_subscriber_node
  collision_event_publisher
  DESTINATION lib/${PROJECT_NAME}
)

# 安装头文件
install(DIRECTORY include/${PROJECT_NAME}/
  DESTINATION include/${PROJECT_NAME}
  FILES_MATCHING PATTERN "*.h"
)

# 安装配置文件（config.yaml）
install(DIRECTORY config/
  DESTINATION share/${PROJECT_NAME}/config
)

# 安装launch文件
install(DIRECTORY src/launch/
  DESTINATION share/${PROJECT_NAME}/launch
  FILES_MATCHING PATTERN "*.py"
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()
